package ThreadSecurity;

import java.util.jar.JarOutputStream;

public class SaleThread2 implements Runnable{

    private int tickets = 10;
    Object lock = new Object();
    @Override
    public void run() {

        while (true){
            // 同步代码块
            synchronized (lock){
                if(tickets > 0){
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"窗口正在出售第"+tickets--+"张票~");
                }
            }
        }
    }

    public static void main(String[] args) {

        SaleThread2 s = new SaleThread2();
        new Thread(s,"窗口1").start();
        new Thread(s,"窗口2").start();
        new Thread(s,"窗口3").start();
        new Thread(s,"窗口4").start();
    }
}
